import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Between } from 'typeorm';
import { SystemMetric } from './entities/system-metric.entity';
import { PerformanceMetric } from './entities/performance-metric.entity';
import { CreateSystemMetricDto } from './dto/create-system-metric.dto';
import { CreatePerformanceMetricDto } from './dto/create-performance-metric.dto';
import { MetricsQueryDto } from './dto/metrics-query.dto';

@Injectable()
export class MetricsService {
  constructor(
    @InjectRepository(SystemMetric)
    private systemMetricRepository: Repository<SystemMetric>,
    @InjectRepository(PerformanceMetric)
    private performanceMetricRepository: Repository<PerformanceMetric>,
  ) {}

  async createSystemMetric(createSystemMetricDto: CreateSystemMetricDto): Promise<SystemMetric> {
    const metric = this.systemMetricRepository.create(createSystemMetricDto);
    return this.systemMetricRepository.save(metric as SystemMetric);
  }

  async createPerformanceMetric(createPerformanceMetricDto: CreatePerformanceMetricDto): Promise<PerformanceMetric> {
    const metric = this.performanceMetricRepository.create(createPerformanceMetricDto);
    return this.performanceMetricRepository.save(metric as PerformanceMetric);
  }

  async getSystemMetrics(query: MetricsQueryDto): Promise<SystemMetric[]> {
    const { startDate, endDate, systemId, region, environment } = query;
    
    const whereCondition: any = {};
    
    if (systemId) {
      whereCondition.systemId = systemId;
    }
    
    if (region) {
      whereCondition.region = region;
    }
    
    if (environment) {
      whereCondition.environment = environment;
    }
    
    if (startDate && endDate) {
      whereCondition.timestamp = Between(new Date(startDate), new Date(endDate));
    }
    
    return this.systemMetricRepository.find({
      where: whereCondition,
      order: { timestamp: 'DESC' },
      take: query.limit || 100,
    });
  }

  async getPerformanceMetrics(query: MetricsQueryDto): Promise<PerformanceMetric[]> {
    const { startDate, endDate, serviceId, region, environment } = query;
    
    const whereCondition: any = {};
    
    if (serviceId) {
      whereCondition.serviceId = serviceId;
    }
    
    if (region) {
      whereCondition.region = region;
    }
    
    if (environment) {
      whereCondition.environment = environment;
    }
    
    if (startDate && endDate) {
      whereCondition.timestamp = Between(new Date(startDate), new Date(endDate));
    }
    
    return this.performanceMetricRepository.find({
      where: whereCondition,
      order: { timestamp: 'DESC' },
      take: query.limit || 100,
    });
  }

  async getSystemMetricById(id: number): Promise<SystemMetric> {
    return this.systemMetricRepository.findOne({ where: { id } });
  }

  async getPerformanceMetricById(id: number): Promise<PerformanceMetric> {
    return this.performanceMetricRepository.findOne({ where: { id } });
  }
} 